package org.example.seata.bs.auth.config;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.example.seata.common.security.config.auth.grant.CustomAuthenticationProvider;
import org.example.seata.common.security.service.CustomUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author hzq
 * @date 2021/12/6 10:41
 */
@Configuration
@AllArgsConstructor
public class WebSecurityAuthorizationConfiguration extends WebSecurityConfigurerAdapter {

    private final CustomUserDetailsService userDetailsService;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authenticationProvider(CustomAuthenticationProvider.build(userDetailsService))
                .authorizeRequests()
                .antMatchers("/sendCode/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf().disable();
    }

    @Bean
    @Override
    @SneakyThrows
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
}
